﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Text
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string regext = "[0-9a-zA-Z]+[0-9a-zA-Z\\.-]*\\.[a-zA-Z]{2,4}";

            if (Regex.IsMatch(this.textBox1.Text, regext))
            {
                MessageBox.Show("OK");
            }
            else
            {
                MessageBox.Show("error");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = Encry(this.textBox1.Text, true);
        }

        private static string Encry(string str, bool bIsEncr)
        {
            byte[] bytAry = Encoding.UTF8.GetBytes(str);
            for (int i = 0; i < (int)(bytAry.Length) - 5; i = i + 5)
            {
                int bytTemp = (int)bytAry[i] + (bIsEncr ? 5 : -5);
                bytAry[i] = (byte)((int)(bytAry[i + 4]) + (bIsEncr ? 5 : -5));
                bytAry[i + 4] = (byte)bytTemp;
            }

            return Encoding.UTF8.GetString(bytAry);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = Encry(this.textBox1.Text, false);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string regex = @"([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+";

            if (Regex.IsMatch(this.textBox1.Text, regex))
            {
                MessageBox.Show("OK");
            }
            else
            {
                MessageBox.Show("error");
            }
        }
    }
}
